package com.mashibing.clouldzuul.filter;

import com.mashibing.internalcommon.constant.RedisKeyPrefixConstant;
import com.mashibing.internalcommon.util.JwtInfo;
import com.mashibing.internalcommon.util.JwtUtil;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.exception.ZuulException;
import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import com.netflix.zuul.context.RequestContext;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author gorkr
 * @date 2022/05/31 11:08
 **/
@Component
public class AuthFilter extends ZuulFilter {

    @Resource
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 该过滤器是否生效 即 是否执行run()
     * @return
     */
    @Override
    public boolean shouldFilter() {
        // 获取上下文
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest request = requestContext.getRequest();

        String uri = request.getRequestURI();
        System.out.println("来源uri:" + uri);
        // 拦截接口
        String checkUri = "/api-passenger/test/auth-test";
        if(checkUri.equalsIgnoreCase(uri)){
            return true;
        }

        return false;
    }

    @Override
    public Object run() throws ZuulException {

        System.out.println("auth 拦截");
        // 获取上下文 贯穿所有filter 包含所有参数
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest request = requestContext.getRequest();

        String token = request.getHeader("Authorization");
        if(StringUtils.isNotBlank(token)){
            JwtInfo jwtInfo = JwtUtil.parseToken(token);

            // jwt存在 即token有效
            if(null != jwtInfo){
                String tokenUserId = jwtInfo.getSubject();
                Long tokenIssueDate = jwtInfo.getIssueDate();

                BoundValueOperations<String, String> stringStringBoundValueOperations = redisTemplate.boundValueOps(RedisKeyPrefixConstant.PASSENGER_LOGIN_TOKEN_APP_KEY_PRE + tokenUserId);
                String redisToken = stringStringBoundValueOperations.get();

                // token存在 放行
                if(redisToken.equals(token)){
                    return null;
                }



            }
        }

        // 否则 拦截

        // 继续走下面的过滤器(order),但是不想服务转发该请求
        requestContext.setSendZuulResponse(false);
        requestContext.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());
        requestContext.setResponseBody("auth fail");


        return null;
    }

    @Override
    public String filterType() {
        // 权限校验一般在pre_type
        return FilterConstants.PRE_TYPE;
    }

    /**
     * 执行顺序 越小越靠前
     * @return
     */
    @Override
    public int filterOrder() {
        return -1;
    }
}
    